package com.achun.ximalayamvvm.view.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.achun.ximalayamvvm.R
import com.achun.ximalayamvvm.databinding.DetailListItemBinding
import com.achun.ximalayamvvm.utils.LogUtil
import com.ximalaya.ting.android.opensdk.model.track.Track
import com.ximalaya.ting.android.opensdk.model.track.TrackList
import java.text.SimpleDateFormat

class DetailListAdapter : RecyclerView.Adapter<DetailListAdapter.InnerHolder>() {
    private var tracks = ArrayList<Track>()

    class InnerHolder(itemview: DetailListItemBinding) :
        RecyclerView.ViewHolder(itemview.root) {
        var binding = itemview
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InnerHolder {
        var binding = DataBindingUtil.inflate<DetailListItemBinding>(
            LayoutInflater.from(parent.context),
            R.layout.detail_list_item, parent, false
        )
        return InnerHolder(binding)
    }

    override fun onBindViewHolder(holder: InnerHolder, position: Int) {
        holder.binding.also {
            tracks[position].run {
                it.soundTitle.text = this.trackTitle
                it.indexTv.text = (position + 1).toString()
                it.dateTv.text = SimpleDateFormat("yyyy-MM-dd").format(this.updatedAt)
                it.playtimeTv.text = SimpleDateFormat("mm:ss").format(this.duration * 1000)
                it.playcountTv.text=this.playCount.toString()
                it.root.setOnClickListener{
                onItemCLickListenter.onclick(position)
                }
            }
        }
    }

    override fun getItemCount(): Int {
        if (tracks == null) return 0 else return tracks.size
    }

    fun setDatas(trackList: List<Track>) {
        tracks = trackList as ArrayList<Track>
        LogUtil.e(this, "条数为--${tracks.size}")
        notifyDataSetChanged()
    }
    private lateinit var onItemCLickListenter: OnItemCLickListenter
    interface OnItemCLickListenter{
        fun onclick(position: Int)
    }
    fun setOnItemClickListerter(onItemCLickListenter: OnItemCLickListenter){
        this.onItemCLickListenter=onItemCLickListenter
    }
}